import { observable, action } from 'mobx'
import axios from '../../../common/request'
import  notificationWithIcon from '../../common/notificationWithIcon';

class InformListStore {
    @observable page={
        currentPage:1,
        pageSize:10
    }
    @observable total
    @observable informs=[]
    @observable selectedRowKeys=[]
    actions = {
        getInform: action((param)=>{
            axios.get('/api/notify/list/'+param+"?pageNumber="+this.page.currentPage+"&pageSize="+this.page.pageSize)
                .then((response)=> {
                    if(response.data.status.code===1) {
                        this.informs=response.data.result.data
                        this.informs.forEach((item)=>{
                            item.key=item.id
                            delete item.id
                        })
                        this.total=response.data.result.total
                    }
                    else{
                        notificationWithIcon('error',response.data.status.message,"")
                    }
                })
                .catch(function (error) {
                    notificationWithIcon('error',"获取消息列表失败",error.response)
                    console.log(error);
                });
        }),
        deleteInform: action(() => {
            axios.delete('/api/notify/delete', {
                params: {
                    notifyId: this.selectedRowKeys.slice()
                }
            })
            .then((response)=> {
                if(response.data.status.code===1){
                    notificationWithIcon('success','删除通知成功',"")
                    this.selectedRowKeys.forEach((key)=>{
                        let index=this.informs.findIndex(item => item.key === key);
                        if(index!=-1)
                            this.informs.splice(index, 1)
                    })
                    this.selectedRowKeys=[]
                }
                else{
                    notificationWithIcon('error',response.data.status.message,"")
                }
            })
            .catch(function (error) {
                notificationWithIcon('error',"操作失败",error.response)
                console.log(error);
            });
        }),
        setSelectKeys:action((select)=>{
            this.selectedRowKeys=select
        }),
        setView:action((expanded,record)=>{
            if(expanded===true){
                if(record.notifyStatus===0){
                    axios.post('/api/notify/read',{notifyId:record.key})
                        .then((response)=> {
                            if(response.data.status.code===1) {
                                this.informs.forEach((item)=>{
                                    if(item.key === record.key){
                                        item.notifyStatus=1
                                    }
                                })
                                let num=document.getElementById("notifyNumber").innerHTML;
                                num--;
                                document.getElementById("notifyNumber").innerHTML=num;
                            }
                            console.log(this.informs)
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }

            }
        }),
        setCurrentPage: action((param,page) => {
            this.page={
                currentPage:page,
                pageSize:this.page.pageSize
            }
            this.actions.getInform(param)
        }),
    }
}

export default new InformListStore()